import os
from time import sleep
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from common.readaccountinfo import ReadConfig
from page.mainpage import MainPage
from common.readpath import ReadPath


class TeacherManagement(MainPage):

    def goto_teachermanagement(self):
        """
        进入侧导航栏排序第一的县级的学校管理
        打开第一所学校管理菜单
        进入教师管理
        :return:
        """
        self.click("css", '#app > div > section > div > div:nth-child(2) > section > ul > li:nth-child(1)')
        self.click("css",
                   '#app > div > section > div > div:nth-child(2) > section > ul > li.secondstage.el-submenu.is-active.is-opened > ul > li:nth-child(1)')
        self.click("xp", "//*[text()='教师管理']")
        sleep(1)

    def locate_addteacher_button(self):
        """
        定位新增教师按钮
        :return:
        """
        self.locate("css","div.options > div.actionsgroup > button:nth-child(3)")

    def locate_importteacher_button(self):
        """
        定位导入教师按钮
        :return:
        """
        self.driver.find_element(By.CSS_SELECTOR, "div.options > div.actionsgroup > button:nth-child(4)")

    def add_teacher(self, role_name, course_name, account, password, picture, teacher_intro, authority):
        """
        点击新增教师新增教师
        输入角色名字
        输入授课名称
        输入账号
        输入密码
        选择照片
        输入教师简介
        选择权限
        :param role_name:教师名称
        :param course_name: 教授课程
        :param account: 账号
        :param password: 密码
        :param picture: 照片
        :param teacher_intro:教师简介
        :param authority: 权限
        :return:
        """
        self.click("css", "div.options > div.actionsgroup > button:nth-child(3)")
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(1) > div > input",
                       role_name)
        self.send_keys("css",
                       "div.main-content > section > section > section > div:nth-child(2) > div > div > div > input",
                       course_name)
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(4) > div > input",
                       account)
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(5) > div > input",
                       password)
        picture_path = os.path.join(ReadPath().get_datapicture_path(), picture)
        # 上传照片(此处上传照片,不能用封装的.send_keys,因为里面包含其他的方法)
        self.driver.find_element(By.NAME, "file").send_keys(picture_path)
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(7) > div > textarea",
                       teacher_intro)
        if authority == "全部权限":
            self.click("css",
                       "div.authority > div > div.el-tree-node.is-expanded.is-focusable > div.el-tree-node__content > label > span > span")
        elif authority == "取消权限":
            self.click("css",
                       "div.authority > div > div.el-tree-node.is-expanded.is-focusable > div.el-tree-node__content > label > span > span")
            self.click("css",
                       "div.authority > div > div.el-tree-node.is-expanded.is-focusable > div.el-tree-node__content > label > span > span")
        elif authority == "默认权限":
            pass
        else:
            print("您选择的权限有误")
        self.click_submit()

    def edit_teacher(self, role_name, course_name, password, picture, teacher_intro, authority):
        """
        选择教师列表第一位教师编辑,且编辑的教师必须拥有全部权限
        输入角色名字
        输入教授课程
        输入密码
        判断原来是否有添加照片:
            如果有:删除原来照片
            如果没有:不执行任何操作
            最后:选择照片
        选择权限
        :param role_name:教师名称
        :param course_name: 教授课程
        :param password: 密码
        :param picture: 照片
        :param teacher_intro:教师简介
        :param authority: 权限
        :return:
        """
        self.click("css",
                   "div.el-table__fixed-body-wrapper > table > tbody > tr:nth-child(1) > td.el-table_1_column_6.is-center > div > button:nth-child(1)")
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(1) > div > input",
                       role_name)
        self.send_keys("css",
                       "div.main-content > section > section > section > div:nth-child(2) > div > div > div > input",
                       course_name)
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(5) > div > input",
                       password)
        try:
            self.driver.find_element(By.CSS_SELECTOR,
                                     "div > div.main-content > section > section > section > div:nth-child(6) > div > i")
        except NoSuchElementException:
            pass
        else:
            self.click("css", "div > div.main-content > section > section > section > div:nth-child(6) > div > i")
        finally:
            picture_path = os.path.join(ReadPath().get_datapicture_path(), picture)
            # 上传照片(此处上传照片,不能用封装的.send_keys,因为里面包含其他的方法)
            self.driver.find_element(By.NAME, "file").send_keys(picture_path)
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(7) > div > textarea",
                           teacher_intro)
        if authority == "取消权限":
            self.click("css",
                       "div.authority > div > div.el-tree-node.is-expanded.is-focusable > div.el-tree-node__content > label > span > span")
        elif authority == "默认权限":
            pass
        else:
            print("您选择的权限有误")
        self.click_submit()

    def delete_num1_teacher(self):
        """
        点击删除列表第一个教师
        :return:
        """
        self.click("css",
                   "div.el-table__fixed-body-wrapper > table > tbody > tr:nth-child(1) > td.el-table_1_column_6.is-center > div > button:nth-child(2)")

    def import_teacher(self, file):
        """
        点击导入教师
        上传文件
        关闭文件上传后的提示框
        点击提交
        :param file:需要上传的文件名
        :return:
        """
        self.click("css", "div.options > div.actionsgroup > button:nth-child(4)")
        file_path = os.path.join(ReadPath().get_datafile_path(), file)
        # 上传文件(此处上传文件,不能用封装的.send_keys,因为里面包含其他的方法)
        self.driver.find_element(By.NAME, "file").send_keys(file_path)
        sleep(1)
        self.close_popup()
        self.click_importpopup_submit()

    def get_num1_teachername(self):
        """
        :return:返回教师列表第一位教师的姓名
        """
        return self.get_text("css",
                             "div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr:nth-child(1) > td.el-table_1_column_1.is-center")

    def get_num1_coursename(self):
        """

        :return:返回教师列表第一位教师的授课名称
        """
        return self.get_text("css",
                             "div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr:nth-child(1) > td.el-table_1_column_3.is-center")

    def get_num1_teacheraccount(self):
        """

        :return:返回教师列表第一位教师的账号
        """
        return self.get_text("css",
                             "div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr:nth-child(1) > td.el-table_1_column_4.is-center")

    def get_num2_teachername(self):
        """

        :return:返回教师列表第二位教师姓名
        """
        return self.get_text("css",
                             "div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr:nth-child(2) > td.el-table_1_column_1.is-center")

    def get_num2_teacheraccount(self):
        """

        :return:返回教师列表第二位教师的账号
        """
        return self.get_text("css",
                             "div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr:nth-child(2) > td.el-table_1_column_4.is-center")


if __name__ == '__main__':
    # 打开谷歌浏览器,输入测试地址
    login = TeacherManagement(ReadConfig().get_http('browsertype'), ReadConfig().get_http('url'))
    # 输入登录账号,密码
    login.login(ReadConfig().get_account('username'), ReadConfig().get_account('password'))
    # 进入电子班牌系统
    login.goto_electroniclassbrand()
    # 进入云控中心(县级管理)
    login.goto_cloudcontrolcenter()
    # 进入教师管理
    login.goto_teachermanagement()
    # login.add_teacher("张三", "数学", "zhangsan001", "123456", "添加教师成功JPG.jpg", "张三是一位好老师", "all")
    login.import_teacher("导入教师成功.xlsx")